package it.koutifaris.travianmap.dal.tile;

import it.koutifaris.travianmap.dal.MapService;
import it.koutifaris.travianmap.dal.RevisionService.Revision;
import it.koutifaris.travianmap.map.WorldMap;
import it.koutifaris.travianmap.map.emptymap.EmptyWorldMap;
import it.koutifaris.travianmap.tile.TileLoader;
import it.koutifaris.travianmap.tile.TileMapLoader;
import it.koutifaris.travianmap.tile.tilemap.TiledWorldMap;

public class TileMapService implements MapService {

	private final TileMapLoader tileMapLoader;

	public TileMapService(TileMapLoader tileMapLoader) {
		this.tileMapLoader = tileMapLoader;
	}

	@Override
	public WorldMap loadMap(Revision revision) {
		WorldMap result;
		TileLoader tileLoader = tileMapLoader.getLoaderFor(revision);

		if (tileLoader == null) {
			result = new EmptyWorldMap(WorldMap.TRAVIAN_MAP_RADIUS);
		} else {
			result = new TiledWorldMap(tileLoader, WorldMap.TRAVIAN_MAP_RADIUS);
		}

		return result;
	}

	@Override
	public void close() {
		tileMapLoader.close();
	}

}
